﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace Infrastructure.WpfClient.Services.Validation
{
    public  class ValidationService : IValidationService
    {
        bool IValidationService.IsVisualObjectValid(object obj, out StringBuilder sb)
        {
            sb = new StringBuilder();
            IsViewValidRecursive((DependencyObject)obj, sb);
            if (sb.Length > 0)
                return false;
            else
                return true;
        }
        protected void IsViewValidRecursive(DependencyObject obj, StringBuilder sb)
        {
            foreach (object child in LogicalTreeHelper.GetChildren(obj))
            {
                var element = child as DependencyObject;
                if (element == null) continue;
                if (System.Windows.Controls.Validation.GetHasError(element))
                {
                    foreach (ValidationError error in System.Windows.Controls.Validation.GetErrors(element))
                    {
                        sb.AppendLine(" " + error.ErrorContent.ToString());
                    }
                }
                this.IsViewValidRecursive(element, sb);
            }
        }
    }
}
